﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MahApps.Metro.Controls;
using Reader.Libs.BOOKOBJ;

namespace Reader.UC.Dialogs
{
    /// <summary>
    /// Interaction logic for PdfPasswordDialog.xaml
    /// </summary>
    public partial class AddBookDialog : MetroWindow
    {
        LibraryUC libUC;
        public AddBookDialog(LibraryUC _libUC)
        {
            InitializeComponent();
            this.libUC = _libUC;
        }

        private void SaveBtn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (BookNameTb.Text=="")
                    throw new Exception("Name is Null");
                if (ArthorTb.Text == "")
                    throw new Exception("Arthor is Null");
                if (PathTb.Text == "")
                    throw new Exception("Path is Null");
                if (!System.IO.File.Exists(PathTb.Text.Trim().ToLower()))
                    throw new Exception("Path is Invalid");

                Book b = new Book();
                b.FullName = BookNameTb.Text;
                b.Arthor = ArthorTb.Text;

                string fileExt = System.IO.Path.GetExtension(PathTb.Text.Trim().ToLower());
                b.FilePath = PathTb.Text.Trim().ToLower();
                switch (fileExt)
                {
                    case ".txt":
                        b.Img = "/Reader;component/Img/book.png";
                        break;
                    case "doc":
                        b.Img = "/Reader;component/Img/book.png";
                        break;
                    case "docx":
                        b.Img = "/Reader;component/Img/book.png";
                        break;
                    case "prc":
                        b.Img = "/Reader;component/Img/book.png";
                        break;
                    case "pdf":
                        b.Img = "/Reader;component/Img/book.png";
                        break;
                    default:
                        b.Img = "/Reader;component/Img/book.png";
                        break;
                }
                this.libUC.AddBook(b);
                this.Close();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Error!\n" + ex.Message);
            }
        }

        private void CancelBtn_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void BrowBtn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Microsoft.Win32.OpenFileDialog OfDlg = new Microsoft.Win32.OpenFileDialog();
                OfDlg.Filter = "Ebook files |*.txt; *.pdf; *.html; *.htm; *.doc; *.docx; *.mobi; *.prc" +
                               "|PDF File |*.pdf" + "|Web Page |*.html; *.htm" +
                               "|Word File |*.doc; *.docx" + "|MobiPocKet |*.prc; *.mobi";
                OfDlg.Multiselect = false;
                OfDlg.ShowDialog();

                this.PathTb.Text = OfDlg.FileName;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Error! " + ex.Message);
            }
        }
    }
}
